package it.koutifaris.travianmap.view.playerdetails;

import it.koutifaris.travianmap.R;
import it.koutifaris.travianmap.context.HasApplicationContex;
import it.koutifaris.travianmap.model.Tribe;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

class PlayerDetailsViewImpl extends HasApplicationContex implements PlayerDetailsView {

	@Override
	public void setPlayerName(String playerName) {
		getPlayerView().setText(playerName);
	}

	private TextView getPlayerView() {
		return (TextView) getActivity().findViewById(R.id.playerdetails_player);
	}

	@Override
	public void setAlliance(String alliance) {
		getAllianceView().setText(alliance);
	}

	private TextView getAllianceView() {
		return (TextView) getActivity().findViewById(R.id.playerdetails_alliance);
	}

	@Override
	public void setTribe(Tribe tribe) {
		getTribeView().setText(tribe.toString());
	}

	private TextView getTribeView() {
		return (TextView) getActivity().findViewById(R.id.playerdetails_tribe);
	}

	@Override
	public ListView getListView() {
		return (ListView) getActivity().findViewById(R.id.playerdetails_villages_listview);
	}

	@Override
	public VillageItemView itemViewFor(View itemView) {
		return new VillageItemViewImpl(itemView);
	}

	@Override
	public View newItemView(ViewGroup parent) {
		LayoutInflater inflater = getInflater();
		return inflater.inflate(R.layout.player_details_village_item, parent, false);
	}

	private LayoutInflater getInflater() {
		return (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	private class VillageItemViewImpl implements VillageItemView {

		private final View androidView;

		public VillageItemViewImpl(View androidView) {
			this.androidView = androidView;
		}

		@Override
		public void setVillageName(String village) {
			getVillageView().setText(village);
		}

		private TextView getVillageView() {
			return (TextView) androidView.findViewById(R.id.playerdetails_item_village);
		}

		@Override
		public void setPopulation(int population) {
			getPopulationView().setText(Integer.toString(population));
		}

		private TextView getPopulationView() {
			return (TextView) androidView.findViewById(R.id.playerdetails_item_population);
		}
	}
}
